package com.example.springboot.service.impl;

import com.example.springboot.mapper.WorkerMapper;
import com.example.springboot.pojo.PageBean;
import com.example.springboot.pojo.Worker;
import com.example.springboot.service.WorkerService;
import com.example.springboot.util.Encrypt;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


@Service
public class WorkerServiceImpl implements WorkerService {

    @Autowired
    private WorkerMapper workerMapper;

    @Override
    public Worker findByWorkerId(String workerId) {
        return workerMapper.findByWorkerId(workerId);
    }

    @Override
    public PageBean<Worker> getAllWorker(Integer pageNum,Integer pageSize) {
        PageBean<Worker> pb = new PageBean<>();
        //开启分页查询
        PageHelper.startPage(pageNum,pageSize);
        //调用mapper
        List<Worker> workers = workerMapper.getAllWorker();
        Page<Worker> p = (Page<Worker>) workers;
        //把数据填充到PageBean对象中
        pb.setTotal(p.getTotal());
        pb.setItems(p.getResult());
        return pb;
    }

    @Override
    public void addWorker(String workerId, String name, String password, String id, String phoneNumber, String department,String email) {
        String encryptPsd = Encrypt.toMD5(password);
        workerMapper.addWorker(workerId,name,encryptPsd,id,phoneNumber,department,email);
    }

    @Override
    public void updateWorker(String workerId, String name, String id, String phoneNumber, String department,String email) {
        workerMapper.updateWorker(workerId,name,id,phoneNumber,department,email);
    }

    @Override
    public void deleteWorker(String workerId) {
        workerMapper.deleteWorker(workerId);
    }

    @Override
    public Worker[] findRepairman() {
        return workerMapper.findRepairman();
    }

    @Override
    public void updatePassword(String email, String password) {
        workerMapper.updatePassword(email,password);
    }

    @Override
    public Worker findByEmail(String email) {
        return workerMapper.findByEmail(email);
    }
}
